//
//  FocusDoctorViewController.m
//  QuickCure
//
//  Created by haoge on 16/9/10.
//  Copyright © 2016年 haoge. All rights reserved.
//

#import "FocusDoctorViewController.h"
#import "DoctorListModel.h"
#import "DoctorListCell.h"
#import "DoctorViewController.h"

@interface FocusDoctorViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSArray<DoctorListModel *> *doctorList;

@property (nonatomic, strong) UITableView *tableView;



@end


static NSString *identifier = @"doctorList";
@implementation FocusDoctorViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setupUI];
    
    [self.tableView registerClass:[DoctorListCell class] forCellReuseIdentifier:identifier];
    
    [DoctorListModel doctorListWithSuccess:^(NSArray<DoctorListModel *> *da) {
        _doctorList = da;
        [self.tableView reloadData];
    } error:^(NSError *error) {
        NSLog(@"error");
    }];
    

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];

}

- (void)setupUI {
    
    self.title = @"关注医生";
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.view);
        make.leading.equalTo(self.view);
        make.trailing.equalTo(self.view);
        make.bottom.equalTo(self.view);
    }];
}
#pragma mark - tableView dataSource & delegate method

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.doctorList.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    DoctorListCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier forIndexPath:indexPath];
    
    DoctorListModel *model = self.doctorList[indexPath.section];
    
    cell.selectionStyle = UITableViewCellSelectionStyleGray;
    
    cell.model = model;
    
    return cell;       
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    DoctorListModel *model = self.doctorList[indexPath.section];
    
    DoctorViewController *docVC = [[DoctorViewController alloc] init];
    docVC.model = model;
    [self.navigationController pushViewController:docVC animated:YES];
    
    // 取消cell 的选中状态
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}


#pragma mark - Lazy Load

- (UITableView *)tableView {
    
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStyleGrouped];
    
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.rowHeight = 100;
        [self.view addSubview:_tableView];
    }
    return _tableView;
}

- (NSArray<DoctorListModel *> *)doctorList {
    
    if (!_doctorList) {
    
        _doctorList = [NSArray array];
    }
    return  _doctorList;
}

@end
